import * as func from "./func.js"
import { round } from "@/common.js"
export default {
  name: '',
  props: {
    formData:{
      type: Array,
      default: () => []
    },
    item: {
      type: Object,
      default: () => {}
    },
    modalType: {
      type: Number,
      default: 0
    }
  },
  components:{},
  data(){
    return {
      calcVal: '',
    }
  },
  computed:{
    formData_item: function(){
      return {formData: this.formData, item: this.item}
    },
  },
  watch:{
    formData_item: {
      handler(newVal, oldVal) {
        const { formData, item } = newVal
        if(item.autoCalc) {
          const formTemplateData = this.getTemplateData(formData)
          let matches = item.autoCalc.match(/\${([^}]*)}/g) // 匹配包含在${}中的字符串
          matches = [...new Set(matches)].map(v => ({ field: v, value: formTemplateData[v.replace(/[${]/g, '').replace(/[}]/g, '')] || ''}))
          if(matches.every(v => v.value != '')) {
            const autoCalc = matches.reduce((prev, item) => prev.replaceAll(item.field, item.value), item.autoCalc)
            this.calcVal = round(eval(autoCalc), item.decimal || 0) || '' // 自动计算数值
          } else {
            this.calcVal = '' // 自动计算数值
          }
        }
      },
      deep: true, // 监听深层对象
      immediate: true, // 会在页面渲染之前， 先执行一遍这个监听
    },
  },
  methods:{
    ...func,
  },
  created(){
    
  },
  mounted(){
    
  },
}